﻿using System;
using System.Runtime.Serialization;
using FarseerPhysics.SamplesFramework;
using Flatland.GeneticProgramming.CoreTypes;
using Flatland.Utilities;
using Microsoft.Xna.Framework;

/**
* 
* Copyright (c) 2012 Anders Høst Kjærgaard | ahkj@itu.dk
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, 
* including without limitation the rights to use, copy, modify, merge, publish, distribute, 
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Flatland.GeneBank {
    [Serializable]
    public class DoTheWalkAbout : GenoType {
        private Vector2 _target = Vector2.Zero;
        private Vector2 _start = Vector2.Zero;
        private int longWalk = 100;

        //Some random dists the agent may walk
        public int[] Dists = new [] { 100, 150, 200, 250 };

        //We regard any walk about just as good as any other
        public override double Fitness() {
            return int.MaxValue;
        }

        public override void Respond(ref string msg) {
            if(!Agent.SeesFood)
            {
                Act(ref msg);
            }
        }

        public override object Clone(){
            return new DoTheWalkAbout();
        }

        protected override void Act(ref string msg) {
            Execute();
        }

        [Primitive]
        public bool ShouldWalkAbout() {
            return true;
        }

        [Primitive]
        public void IncreaseLongWalk() {
            longWalk = longWalk++ % int.MaxValue;
        }

        [Primitive]
        public void DecreaseLongWalk() {
            Math.Abs(--longWalk);
        }

        public bool HasReachedTarget()
        {
            double dist =
    Math.Sqrt(Math.Pow(ConvertUnits.ToDisplayUnits(Agent.Body.Position.X - _target.X), 2) +
              Math.Pow(ConvertUnits.ToDisplayUnits(Agent.Body.Position.Y - _target.Y), 2));
            return dist < 1;
        }

        [Primitive]
        public void ResetHorizontalTarget() {
            float x = Utils.Random.Next(Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferWidth);
            float y = _target.Y;
            _target = new Vector2(x, y);
            _start = Agent.Body.Position;
        }

        [Primitive]
        public void ResetVerticalTarget() {
            float x = _target.X;
            float y = Utils.Random.Next(Program.FlatlandWorld.GraphicsDeviceManager.PreferredBackBufferHeight);
            _target = new Vector2(x, y);
            _start = Agent.Body.Position;
        }

        [Primitive]
        public bool HasWalkedForALongTime() {
            double dist =
                Math.Sqrt(Math.Pow(ConvertUnits.ToDisplayUnits(Agent.Body.Position.X - _start.X), 2) +
                          Math.Pow(ConvertUnits.ToDisplayUnits(Agent.Body.Position.Y - _start.Y), 2));
            return dist < longWalk;
        }

        [Primitive]
        public void WalkAbout() {
            Agent.Move();
        }

        [Primitive]
        public void WalkNTurn() {
            Agent.Move();
            Agent.TurnRight();
        }

        [Primitive]
        public void WalkNTurn2() {
            Agent.Move();
            Agent.TurnLeft();
        }

        [Primitive]
        public void WalkNTurn3() {
            Agent.Move();
            Agent.TurnLeft();
            Agent.TurnLeft();
        }

        [Primitive]
        public void LookAround() {
            Agent.Stop();
            Agent.TurnLeft();
        }

        public DoTheWalkAbout(SerializationInfo info, StreamingContext context) {}
        public DoTheWalkAbout() { }
    }
}
